package com.framework.loippi.service.impl;

import com.framework.loippi.dao.TMedicineDao;
import com.framework.loippi.entity.TMedicine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.framework.loippi.dao.TPreferentialDao;
import com.framework.loippi.entity.TPreferential;
import com.framework.loippi.service.TPreferentialService;
import sun.awt.image.BufImgSurfaceData;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * SERVICE - T preferential(优惠比例记录表)
 *
 * @author wmj
 * @version 2.0
 */
@Service
public class TPreferentialServiceImpl extends GenericServiceImpl<TPreferential, Long> implements TPreferentialService {

    @Autowired
    private TPreferentialDao tPreferentialDao;

    @Autowired
    private TMedicineDao medicineDao;


    @Autowired
    public void setGenericDao() {
        super.setGenericDao(tPreferentialDao);
    }

    public List<TPreferential> findListByPage(Object parameter) {
        return tPreferentialDao.findListByPage(parameter);
    }

    public Long deletes(Object parameter) {
        return tPreferentialDao.deletes(parameter);
    }

    public TPreferential findByUidGid(Long uid,Long gid){

        return tPreferentialDao.findByUidGid(uid,gid);
    }

    /**
     * 被推荐人购买成功以后 更新推荐人优惠比例
     * @param uid
     * @param gid
     */
    @Override
    public void updateByUidAndGid(Long uid, Long gid) {
        TPreferential preferential = findByUidGid(uid,gid);
        String uidStr = String.valueOf(uid);
        String gidStr = String.valueOf(gid);
        TMedicine medicine = medicineDao.find(gid);
        if(null==preferential){
            try {
                //表中没有当前用户对应药物的优惠记录 执行插入操作
                TPreferential newPreferential = new TPreferential();
                newPreferential.setGid(Integer.valueOf(gidStr));
                newPreferential.setUid(Integer.valueOf(uidStr));
                newPreferential.setPreferential(medicine.getPreferential());
                tPreferentialDao.insertRecord(newPreferential);
            }catch (Exception e){
                System.err.println("插入药品优惠信息失败");
                e.printStackTrace();
            }

        }else{
            try{
                //表中存在用户优惠信息记录 执行更新操作
                BigDecimal currPercentage = preferential.getPreferential();
                BigDecimal  maxPercentage = new BigDecimal(0.5).setScale(3,RoundingMode.HALF_UP);
                BigDecimal increment = medicine.getPreferential();
                BigDecimal add = increment.add(currPercentage).setScale(3,RoundingMode.HALF_UP);

                //比较当前优惠比例  如果大于50 则取值优惠比例为50（数据库存储为0.500）,小于则取累加值
                int result = add.compareTo(maxPercentage);
                if(result==1||result==0){
                    //大于等于50
                    preferential.setPreferential(maxPercentage);
                }
                if(result==-1){
                    //小于50
                    preferential.setPreferential(add);
                }
                //更新
                tPreferentialDao.updateByUidAndGid(preferential);
            }catch (Exception e){
                System.err.println("更新药品优惠信息失败");
            }
        }
    }
}
